package Scaner.rfid;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.speedata.libuhf.IUHFService;
import com.speedata.libuhf.UHFManager;
import com.speedata.libuhf.bean.SpdInventoryData;
import com.speedata.libuhf.interfaces.OnSpdInventoryListener;

import java.util.ArrayList;
import java.util.List;

import util.StringUtils;

/**
 * Created by zhengjinglan on 2018/5/27.
 */

public class TimerRfidScaner extends RfidScan {

    private IUHFService iuhfService;
    private SoundPool soundPool;
    private List<EpcDataBase> firm = new ArrayList<EpcDataBase>();
    private Context context;
    private String TAG = "TimerRfidScaner";
    private int soundId;
    private long scant = 0;
    private boolean isSound = true;

    private boolean isSearching = false; // 是否在搜索中

    /**
     * 设备是否是在扫描搜索中
     *
     * @return
     */
    public boolean isSearching() {
        return isSearching;
    }

    public TimerRfidScaner(Context context) {
        this.context = context;
        init();
    }

    @Override
    public void init() {

        try {
            //判断模块实例化
            iuhfService = UHFManager.getUHFService(context);
        } catch (Exception e) {
            e.printStackTrace();
            SearchException("模块不识别");
            return;
        }

        try {
            if (iuhfService != null) {
                iuhfService.setOnInventoryListener(null);
                //设备上电
                iuhfService.setOnInventoryListener(new OnSpdInventoryListener() {
                    @Override
                    public void getInventoryData(SpdInventoryData var1) {
                        handler.sendMessage(handler.obtainMessage(1, var1));
                    }
                });
                if (openDev()) return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            SearchException("设备上电失败");
        }
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        if (soundPool == null) {
            SearchException("as3992:Open sound failed");
        }
        soundId = soundPool.load("/system/media/audio/ui/VideoRecord.ogg", 0);


    }

    @Override
    public void StartSearch() {
        scant = 0;
        firm.clear();
        iuhfService.inventoryStart();
        this.isSearching = true;
    }

    @Override
    public void StopSearch() {
        Log.e(TAG, "stop: im stopping");
        if (iuhfService != null) {
            iuhfService.inventoryStop();
        }
        this.isSearching = false;
    }

    @Override
    public void Remove() {
        firm.clear();
        rfidListener.onRfiding(firm);
    }

    @Override
    public void Destory() {
        try {
            if (iuhfService != null) {
                //设备下电
                iuhfService.closeDev();
                iuhfService.setOnInventoryListener(null);
            }

            isSearching = false;
        } catch (Exception e) {
            e.printStackTrace();
            rfidListener.getException("设备下电失败");
        }
        if (soundPool != null) {
            soundPool.release();
        }
    }

    @Override
    public void SearchException(String msg) {
        if (rfidListener != null) {
            rfidListener.getException(msg);
        }
    }

    @Override
    public void SetRfid(int rfid_name, int antenna_power, Boolean soundPool) {
        //设置声音
        isSound = soundPool;
    }


    public class EpcDataBase {
        String epc;
        int valid;
        String rssi;
        String tid_user;

        public EpcDataBase(String e, int v, String rssi, String tid_user) {
            epc = e;
            valid = v;
            this.rssi = rssi;
            this.tid_user = tid_user;
        }

        public String getRssi() {
            return rssi;
        }

        public void setRssi(String rssi) {
            this.rssi = rssi;
        }

        @Override
        public String toString() {
            return epc;
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                scant++;
                if (scant % 10 == 0) {
                    soundPool.play(soundId, 1, 1, 0, 0, 1);
                }
                SpdInventoryData var1 = (SpdInventoryData) msg.obj;
                int j;
                for (j = 0; j < firm.size(); j++) {
                    if (var1.epc.equals(firm.get(j).epc)) {
                        firm.get(j).valid++;
                        firm.get(j).setRssi(var1.rssi);
                        break;
                    }
                }
                if (j == firm.size()) {
                    String result = StringUtils.replaceBlank(var1.epc);
                    if (result != null && result.length() > 8) {
                        firm.add(new EpcDataBase(result.substring(result.length() - 8), 1,
                                StringUtils.replaceBlank(var1.rssi),
                                StringUtils.replaceBlank(var1.tid)));
                    } else {
                        firm.add(new EpcDataBase(result, 1, StringUtils.replaceBlank(var1.rssi),
                                StringUtils.replaceBlank(var1.tid)));
                    }

                }
                if (rfidListener != null) {
                    rfidListener.onRfiding(firm);
                }
            }
        }
    };

    private boolean openDev() {
        //设备上电， 0表示成功， -1表示失败
        if (iuhfService.openDev() != 0) {
            return true;
        }
        return false;
    }
}
